#include "uni_hal_i2c.h"
#include "i2c.h"
#include "DrvUART010.h"

int uni_i2c_init(i2c_clock clock)
{
     if((clock != I2C_CLOCK_100K) && (clock != I2C_CLOCK_400K))
     {
           fLib_printf("uni_i2c_init clock error\r\n");
		   return -1;
     }
     i2c_init();
	 if(I2C_CLOCK_100K == clock )
	     I2C_SetSCLout(IIC_FTIIC010_0_PA_BASE,100);//100k
	 else
	 	 I2C_SetSCLout(IIC_FTIIC010_0_PA_BASE,400);//400k

	 return 0;
}

int uni_i2c_write(u8 slave_addr, u8 * buf, u32 length)
{
    return i2c_write(slave_addr,buf,length,1);

}
//if read reg value ,buf[0] is reg addr,else buf[0] is 0
int uni_i2c_read(u8 slave_addr, u8* buf, u32 length)
{
    int ret = -1;
	if(buf[0])
         ret = i2c_write(slave_addr,buf,1,0);//send reg addr
    else
		 ret = i2c_write(slave_addr,buf,0,0);//only send slave addr
    if(!ret)
       return i2c_read(slave_addr, buf, length);

	return ret; 
}
